package com.jrx.anytxn.feign.aspect;

import com.jrx.anytxn.common.uitl.BeanUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @program: anytxn-cf-feign
 * @description: fegin接口切面
 * @author: xiaoyu.jing
 * @date: 2019-11-25 21:55
 **/
@Component
@Aspect
public class FeginAspect {


    private static Logger logger = LoggerFactory.getLogger(FeginAspect.class);

    /**
     * gateway网关接口切点
     */
    @Pointcut("execution(* com.jrx.anytxn.feign.gateway.service.IInnerGatewayService.*(..))")
    public void pointCutGateWay() {
    }

    @Around(value = "pointCutGateWay()")
    public Object aroundGateWay(ProceedingJoinPoint pjp) throws Throwable {
        String methodName = pjp.getSignature().getName();
        Object[] objects = pjp.getArgs();
        logger.info("feign 网关接口{}调用执行开始,请求参数:{}",methodName, objects);
        //执行并获取返回参数
        Object result = pjp.proceed();
        logger.info("feign 网关接口{}调用执行完成,返回参数:{}",methodName,BeanUtils.toMap(result));
        return result;
    }

    /**
     * user接口切点
     */
    @Pointcut("execution(* com.jrx.anytxn.feign.api.service.IUserApiService.*(..))")
    public void pointCutUser() {
    }

    @Around(value = "pointCutUser()")
    public Object aroundUser(ProceedingJoinPoint pjp) throws Throwable {
        String methodName = pjp.getSignature().getName();
        Object[] objects = pjp.getArgs();
        logger.info("feign user接口{}调用执行开始,请求参数:{}",methodName, objects);
        //执行并获取返回参数
        Object result = pjp.proceed();
        logger.info("feign user接口{}调用执行完成,返回参数:{}",methodName,BeanUtils.toMap(result));
        return result;
    }

    /**
     * param接口切点
     */
    @Pointcut("execution(* com.jrx.anytxn.feign.param.service.IAnyTxnParamService.*(..))")
    public void pointCutParam() {
    }

    @Around(value = "pointCutParam()")
    public Object aroundParam(ProceedingJoinPoint pjp) throws Throwable {
        String methodName = pjp.getSignature().getName();
        Object[] objects = pjp.getArgs();
        logger.info("feign param接口{}调用执行开始,请求参数:{}",methodName, objects);
        //执行并获取返回参数
        Object result = pjp.proceed();
        logger.info("feign param接口{}调用执行完成,返回参数:{}",methodName,BeanUtils.toMap(result));
        return result;
    }

    /**
     * investor接口切点
     */
    @Pointcut("execution(* com.jrx.anytxn.feign.investor.service.IAnyTxnInvestorService.*(..))")
    public void pointCutInvestor() {
    }

    @Around(value = "pointCutInvestor()")
    public Object aroundInvestor(ProceedingJoinPoint pjp) throws Throwable {
        String methodName = pjp.getSignature().getName();
        Object[] objects = pjp.getArgs();
        logger.info("feign investor接口{}调用执行开始,请求参数:{}",methodName, objects);
        //执行并获取返回参数
        Object result = pjp.proceed();
        logger.info("feign investor接口{}调用执行完成,返回参数:{}",methodName,BeanUtils.toMap(result));
        return result;
    }

    /**
     * transaction接口切点
     */
    @Pointcut("execution(* com.jrx.anytxn.feign.transaction.service.IAnyTxnTransactionService.*(..))")
    public void pointCutTransaction() {
    }

    @Around(value = "pointCutTransaction()")
    public Object aroundTransaction(ProceedingJoinPoint pjp) throws Throwable {
        String methodName = pjp.getSignature().getName();
        Object[] objects = pjp.getArgs();
        logger.info("feign transaction接口{}调用执行开始,请求参数:{}",methodName, objects);
        //执行并获取返回参数
        Object result = pjp.proceed();
        logger.info("feign transaction接口{}调用执行完成,返回参数:{}",methodName,BeanUtils.toMap(result));
        return result;
    }
}
